package com.red.wood.module.eval.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.eval.dao.EvaluationInfoDao;

@Repository
public class EvaluationInfoDaoImpl implements EvaluationInfoDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public Map<String, Object> getPageData(Map<String, Object> param){
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}

	public String listCount(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) as count from t_evaluationInfo where 1=1");
		sql.append(getQueryconf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id AS id,corporateNo AS corporateNo,corporateName AS corporateName, formNum AS formNum,operateBusiness AS operateBusiness, operateProvince AS operateProvince, applyBusiness AS applyBusiness, formState AS formState,registerLocal AS registerLocal, constructionDept AS constructionDept, xaHandleAbility AS xaHandleAbility, houseNum AS houseNum, gameAddress AS gameAddress, houseName AS houseName, houseAddress AS houseAddress, linkGbps AS linkGbps FROM t_evaluationInfo WHERE 1=1");
		sql.append(getQueryconf(param));
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public StringBuffer getQueryconf(Map<String, Object> param) {
		StringBuffer queryConf = new StringBuffer();
		String corporateNo = CommonUtils.valueOf(param.get("corporateNo")); //企业许可证号，模糊匹配
		if(StringUtils.isNotBlank(corporateNo)){
			queryConf.append(" and corporateNo like concat('%', '"+corporateNo+"', '%')");
		}
		String applyBusiness = CommonUtils.valueOf(param.get("applyBusiness")); // 申请业务
		if(StringUtils.isNotBlank(applyBusiness)){
			queryConf.append(" and applyBusiness='"+applyBusiness+"'");
		}
		String corporateName = CommonUtils.valueOf(param.get("corporateName")); //企业名称
		if(StringUtils.isNotBlank(corporateName)){
			queryConf.append(" and corporateName like concat('%', '"+corporateName+"', '%')");
		}
		String formNum = CommonUtils.valueOf(param.get("formNum")); // 表单编号
		if(StringUtils.isNotBlank(formNum)){
			queryConf.append(" and formNum like concat('%', '"+formNum+"', '%')");
		}
		String formState = CommonUtils.valueOf(param.get("formState")); // 表单状态
		if(StringUtils.isNotBlank(formState)){
			queryConf.append(" and formState like concat('%', '"+formState+"', '%')");
		}
		String registerLocal = CommonUtils.valueOf(param.get("registerLocal")); // 注册地
		if(StringUtils.isNotBlank(registerLocal)){
			queryConf.append(" and registerLocal like concat('%', '"+registerLocal+"', '%')");
		}
		return queryConf;
	}
	
	@Override
	public List<Map<String, Object>> getDetailInfo(String houseId) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
